package vip.xiaonuo.smzq.modular.lifecycle.service.module.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.smzq.modular.lifecycle.base.KqfwCoordinates;
import vip.xiaonuo.smzq.modular.lifecycle.constant.LifeCycleConstant;
import vip.xiaonuo.smzq.modular.lifecycle.entity.*;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.entity.KclCb06KcMapper;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.entity.KclKsMapper;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.entity.KclZbMapper;
import vip.xiaonuo.smzq.modular.lifecycle.po.datamanager.CreateAreaImgPO;
import vip.xiaonuo.smzq.modular.lifecycle.po.datamanager.QueryMineListPO;
import vip.xiaonuo.smzq.modular.lifecycle.service.entity.LifeCycleMineStoreService;
import vip.xiaonuo.smzq.modular.lifecycle.service.entity.LifeCycleMineralMeaService;
import vip.xiaonuo.smzq.modular.lifecycle.service.module.*;
import vip.xiaonuo.smzq.modular.lifecycle.util.CoordinateTranUtil;
import vip.xiaonuo.smzq.modular.lifecycle.util.HutoolExtend;
import vip.xiaonuo.smzq.modular.lifecycle.vo.PolygonWgs84V0;
import vip.xiaonuo.smzq.modular.lifecycle.vo.QueryMineInfoVO;
import vip.xiaonuo.smzq.modular.lifecycle.vo.ThreeDHeightVO;
import vip.xiaonuo.smzq.modular.lifecycle.vo.ThreeWeftCoorVO;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <ul>
 * <li>name:  InDBServiceImpl</li>
 * <li>author name: GEOSCENE_YUYANFEI</li>
 * <li>create time: 2024-03-20 15:31:25</li>
 * </ul>
 */
@Service
public class InDBServiceImpl implements InDBService {

    @Autowired
    LifeCycleDataManagerService lifeCycleDataManagerService;

    @Autowired
    KclKsMapper kclKsMapper;

    @Autowired
    KclCb06KcMapper kclCb06KcMapper;

    @Autowired
    KclZbMapper kclZbMapper;

    @Autowired
    LifeCycleService lifeCycleService;

    @Autowired
    LifeCycleMineStoreService lifeCycleMineStoreService;

    @Autowired
    LifeCycleMineralMeaService lifeCycleMineralMeaService;

    @Autowired
    LifeCycleHanderDataService lifeCycleHanderDataService;


    @Override
    public List<QueryMineInfoVO> queryAllLCMineList() {

        MPJLambdaWrapper<KclKs> wrapperAll = new MPJLambdaWrapper<KclKs>()
                .selectAll(KclKs.class);
        wrapperAll = lifeCycleDataManagerService.getKclListWrapper(wrapperAll, new QueryMineListPO());

        List<QueryMineInfoVO> queryMineInfoVOS = kclKsMapper.selectJoinList(QueryMineInfoVO.class, wrapperAll);

        return lifeCycleService.getKsExportType(queryMineInfoVOS);
    }

    @Override
    public void saveAllTkqToWkqLcNode() {
        List<QueryMineInfoVO> allList = queryAllLCMineList();
//        过滤所有的探矿权数据
        List<QueryMineInfoVO> tkqList = allList.stream().filter(ele-> StrUtil.equals(ele.getMineObligeeType(),"探矿权")).collect(Collectors.toList());

//        给每一条探矿权数据,在数据库生成一条

//        System.out.println(list);
        for (QueryMineInfoVO queryMineInfoVO:tkqList) {
            LifeCycleMineStore lifeCycleMineStore = tranToLcNodeObj(queryMineInfoVO);
            List<LifeCycleMineStore> list = lifeCycleMineStoreService.list(new LambdaQueryWrapper<LifeCycleMineStore>()
                    .eq(LifeCycleMineStore::getMineralCode,queryMineInfoVO.getKqbh())
                    .eq(LifeCycleMineStore::getRegClassCode, queryMineInfoVO.getDjflbh())
                    .eq(LifeCycleMineStore::getCpTkqToWkq,true)
            );
            if(CollUtil.isEmpty(list)){
                lifeCycleMineStoreService.save(lifeCycleMineStore);

                LifeCycleMineralMea lifeCycleMineralMea = getLCMineralMeaObj(lifeCycleMineStore.getId(),queryMineInfoVO.getZkcm(),queryMineInfoVO.getTjdx(),0d);

                lifeCycleMineralMeaService.save(lifeCycleMineralMea);
            }

        }
    }


    private LifeCycleMineralMea getLCMineralMeaObj(String lcNodeId, Integer zkcm,Integer tjdx,Double storeVal){
        LifeCycleMineralMea result = new LifeCycleMineralMea();
        result.setMineStoreId(lcNodeId);
        result.setMineralType(zkcm);
        result.setStoreVal(storeVal);
        result.setTypeStand(3);
        result.setTjdx(tjdx);
        result.setSaveTime(Convert.toLocalDateTime(DateUtil.now()));
        List<KclCb06Kc> kclCb06Kcs = kclCb06KcMapper.selectList(new LambdaQueryWrapper<KclCb06Kc>().eq(KclCb06Kc::getKcdm,zkcm).eq(KclCb06Kc::getTjdx,tjdx));
        if(CollUtil.size(kclCb06Kcs)!=0){
            KclCb06Kc kclCb06Kc = CollUtil.getFirst(kclCb06Kcs);
            result.setKclb(kclCb06Kc.getKclb());
        }

//        lifeCycleMineralMea

        return result;
    }

    private LifeCycleMineStore tranToLcNodeObj(QueryMineInfoVO queryMineInfoVO){
        LifeCycleMineStore result = new LifeCycleMineStore();
//        设置为无矿权
        result.setExporeType(Convert.toInt(CollUtil.getFirst(LifeCycleConstant.mineTypeCodes)));
        result.setRegClassCode(queryMineInfoVO.getDjflbh());
        result.setMineralCode(queryMineInfoVO.getKqbh());
        result.setCpTkqToWkq(true);
        result.setReportName("未利用");

        Integer year = getXkzhYear(queryMineInfoVO.getKqbh(),queryMineInfoVO.getDjflbh());

        result.setYear(year);
        if(ObjectUtil.isNotEmpty(year)){
            result.setStoreDate(Convert.convert(LocalDate.class,year+"-12-31"));
        }else{
            System.out.println("不存在许可证号的情况的数据:"+queryMineInfoVO.getKqbh()+"_"+queryMineInfoVO.getDjflbh());
        }


//        设置保存时间
        result.setSaveTime(Convert.toLocalDateTime(DateUtil.now()));
        result.setApproveStatus(1);
        return result;
    }

    /**
     * 根据矿权编号，和登记分裂编号查询出矿山，如果有许可证号的，得到该年
     * @param mineralCode
     * @param regClassCode
     * @return
     */
    private Integer getXkzhYear(String mineralCode,Integer regClassCode){
        Integer year = null;
       List<KclKs> ksList =  kclKsMapper.selectList(new LambdaQueryWrapper<KclKs>()
                .eq(KclKs::getKqbh,mineralCode)
                .eq(KclKs::getDjflbh,regClassCode)
        );

        for (KclKs kclKs:ksList) {
            if(StrUtil.isNotBlank(kclKs.getXkzh())){
                year = kclKs.getNd();
            }
        }
        return year;

    }


    @Override
    public void saveAllAnnualReport() {
        List<QueryMineInfoVO> allList = queryAllLCMineList();
        for (QueryMineInfoVO queryMineInfoVO:allList) {
            List<LifeCycleMineStore> list = lifeCycleMineStoreService.list(new LambdaQueryWrapper<LifeCycleMineStore>()
                    .eq(LifeCycleMineStore::getMineralCode,queryMineInfoVO.getKqbh())
                    .eq(LifeCycleMineStore::getRegClassCode, queryMineInfoVO.getDjflbh())
                    .eq(LifeCycleMineStore::getCpTkqToWkq,false)
            );
            /*如果不存在该矿山的节点*/
            if(CollUtil.isEmpty(list)){
                List<KclKs> ksList =  kclKsMapper.selectList(new LambdaQueryWrapper<KclKs>()
                        .eq(KclKs::getKqbh,queryMineInfoVO.getKqbh())
                        .eq(KclKs::getDjflbh,queryMineInfoVO.getDjflbh())
                );

                for (KclKs kclKs:ksList) {
                    QueryMineInfoVO queryMineInfoVO1 = BeanUtil.copyProperties(kclKs,QueryMineInfoVO.class);

                    LifeCycleMineStore lifeCycleMineStore = tranToAnnualReportLcNodeObj(queryMineInfoVO1);

                    lifeCycleMineStoreService.save(lifeCycleMineStore);
                    lifeCycleHanderDataService.saveCycleLifeMineralMea(lifeCycleMineStore);
                    createCoorImg(lifeCycleMineStore);
                }

            }



        }
    }
    /*生成坐标图片*/
    public void createCoorImg(LifeCycleMineStore lifeCycleMineStore){
        if(StrUtil.isNotBlank(lifeCycleMineStore.getDmlCoor())){
            CreateAreaImgPO createAreaImgPO =  lifeCycleDataManagerService.createMineField(lifeCycleMineStore.getDmlCoor(),Convert.toStr(lifeCycleMineStore.getExporeType()),lifeCycleMineStore.getId());
            lifeCycleMineStore.setMineralAreaPath(createAreaImgPO.getImgPath());
            lifeCycleMineStoreService.updateById(lifeCycleMineStore);
        }
    }

    /**
     * 转换年报的生命周期节点
     * @param queryMineInfoVO
     * @return
     */
    private LifeCycleMineStore tranToAnnualReportLcNodeObj(QueryMineInfoVO queryMineInfoVO){
        LifeCycleMineStore result = new LifeCycleMineStore();
//        设置为无矿权
        result.setExporeType(Convert.toInt(CollUtil.getLast(LifeCycleConstant.mineTypeCodes)));
        result.setRegClassCode(queryMineInfoVO.getDjflbh());
        result.setMineralCode(queryMineInfoVO.getKqbh());
        result.setCpTkqToWkq(false);
        result.setReportName("年报");

        result.setYear(queryMineInfoVO.getNd());
        if(ObjectUtil.isNotEmpty(queryMineInfoVO.getNd())){
            result.setStoreDate(Convert.convert(LocalDate.class,queryMineInfoVO.getNd()+"-12-31"));
        }else{
            System.out.println("不存在许可证号的情况的数据:"+queryMineInfoVO.getKqbh()+"_"+queryMineInfoVO.getDjflbh());
        }

        LambdaQueryWrapper wrapper =new LambdaQueryWrapper<KclZb>().eq(KclZb::getDjflbh, queryMineInfoVO.getDjflbh())
                .eq(KclZb::getNd, queryMineInfoVO.getNd())
                .eq(KclZb::getKqbh, queryMineInfoVO.getKqbh());
        List<KclZb> kclZbs = kclZbMapper.selectList(wrapper);
        if(CollUtil.isNotEmpty(kclZbs)){

            KclZb kclZb = CollUtil.getFirst(kclZbs);
            result.setDmlCoor(kclZb.getKqzb());

            result.setCalCoor(kclZb.getJszb());
            result.setMomCoor(kclZb.getDlzb());
        }
//        设置保存时间
        result.setSaveTime(Convert.toLocalDateTime(DateUtil.now()));
        result.setApproveStatus(1);
        return result;
    }




}


